package com.hanoi.wiki.controller.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hanoi.wiki.controller.RatesController;
import com.hanoi.wiki.exception.FunctionalException;
import com.hanoi.wiki.manager.RatesManager;
import com.hanoi.wiki.model.json.BaseJsonObject;
import com.hanoi.wiki.model.json.RatesJson;
import com.hanoi.wiki.model.json.ResponseJsonObject;

@Controller
@RequestMapping("/rts")
public class RatesControllerImpl extends BaseControllerImpl implements RatesController{

	@Autowired
	private RatesManager ratesManager;
	
	public @ResponseBody ResponseJsonObject saveRates(@RequestBody RatesJson rates) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		int id = 0;
		try {
			id = ratesManager.save(rates);
		} catch (FunctionalException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		rates.setId(id);
		objects.add(rates);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject removeRates(@PathVariable int id) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		RatesJson json = null;
		
		try {
			json = ratesManager.get(id);
			ratesManager.remove(id);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		objects.add(json);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject updateRates(@RequestBody RatesJson rates) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		try {
			ratesManager.update(rates);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		objects.add(rates);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject getRates(@PathVariable int id) {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		RatesJson json = null;
		try {
			json = ratesManager.get(id);
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		objects.add(json);
		jsonObject.setResults(objects);
		
		return jsonObject;
	}

	public @ResponseBody ResponseJsonObject getRatesList() {
		ResponseJsonObject jsonObject = new ResponseJsonObject();
		List<BaseJsonObject> objects = new ArrayList<BaseJsonObject>();
		
		try {
			objects.addAll(ratesManager.getList());
		} catch (FunctionalException e) {
			e.printStackTrace();
		}
		
		jsonObject.setResults(objects);
		
		return jsonObject;
	}
	
}
